package asia.doing.service.common;

import asia.doing.common.utils.TokenUtils;
import asia.doing.common.utils.UUIDUtils;
import asia.doing.entity.Account;
import asia.doing.entity.Collect;
import asia.doing.mapper.CollectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service(value = "commonCollectService")
public class CommonCollectService {
    @Resource
    private CollectMapper collectMapper;

    public void set(Collect collect){
        Account currentUser = TokenUtils.getCurrentUser();
        collect.setUser_id(currentUser.getId());
        Collect dblCollect = collectMapper.selectUserCollect(collect);
        if(dblCollect == null){
            // 使用uuid作为唯一性的id进行插入数据
            collect.setId(UUIDUtils.getUUIDInOrderId());
            //没有点赞就给它点赞
            collectMapper.insert(collect);
        }else{
            collectMapper.deleteById(dblCollect.getId());
        }
    }

    public Collect selectUserCollect(Long fid, String module){
        Account currentUser = TokenUtils.getCurrentUser();
        Collect collect = new Collect();
        collect.setUser_id(currentUser.getId());
        collect.setFid(fid);
        collect.setModule(module);
        return collectMapper.selectUserCollect(collect);
    }

    public int selectByFidAndModule(Long fid, String module){
        return collectMapper.selectByFidAndModule(fid,module);
    }



}
